import { merge } from 'webpack-merge'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import base from './config.base'
import paths from './paths'
import { Configuration } from 'webpack'

const prodConfig: Configuration = {
  mode: 'production',
  entry: {
    vendor: ['react', 'react-dom'],
    index: paths.src + '/main.tsx',
  },

  devtool: 'hidden-source-map',
  module: {
    rules: [
      {
        test: /\.(css|less)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'less-loader',
            options: {
              modifyVars: {
                'brand-primary': '#282C2D',
              },
            },
          },
          {
            loader: 'style-resources-loader',
            options: {
              patterns: paths.less,
            },
          },
        ],
      },
    ],
  },
  optimization: {
    splitChunks: {
      chunks: 'async',
      minSize: 30000,
      minChunks: 2,
      maxAsyncRequests: 5,
      maxInitialRequests: 3,
      name: false,
      cacheGroups: {
        vendor: {
          chunks: 'all',
          name: 'common',
          test: /[\\/]node_modules[\\/]/,
          enforce: true,
        },
      },
    },
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'assets/css/[name].css',
      chunkFilename: 'assets/css/[name]/[contenthash:12].css',
    }),
  ],
}

export default merge(base, prodConfig)
